package ui.panel {

	import com.greensock.plugins.VolumePlugin;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.text.TextField;
	
	import globals.Global;
	import globals.Setting;
	
	import ui.component.Button;
	
	import util.DisplayUtil;
	import util.UIUtil;
	
	public class BasePanel extends Sprite {
		
		public var pid:uint;
		protected var title:TextField;
		protected var titleBg:Bitmap;
		protected var bg:Bitmap;
		protected var closeButton:Button;
		public var opened:Boolean;

		public function BasePanel() {
			super();

			initUI();
		}
		
		protected function initUI():void {
			opened = false;
			bg = new Bitmap();
			addChildAt(bg, 0);
			
			titleBg = new Bitmap();
			addChild(titleBg);
			
			title = DisplayUtil.getTitle();
			title.x = 210;
			title.y = 4;
			addChild(title);
			
			updateBg("panel_bg");
			
			closeButton = UIUtil.getButton(["panel_close", "panel_close", "panel_close"], closeHandler);
			closeButton.x = 617;
			closeButton.y = 28;
			addChild(closeButton);
		}
		
		private function closeHandler(button:Button):void {
			close();
		}
		
		protected function updateTitle(titleName:String):void {
			titleBg.bitmapData = Global.assetsManager.getAssetsAsBitmapData("title_bg");
			titleBg.x = int((width - titleBg.width) / 2);
			titleBg.y = -5;

			title.text = titleName;
		}
		
		protected function updateBg(bgName:String):void {
			bg.bitmapData = Global.assetsManager.getAssetsAsBitmapData(bgName);
		}
		
		public function open(center:Boolean = true):void {
			if (center) {
				x = int((Setting.STAGE_WIDTH - width) / 2);
				y = int((Setting.STAGE_HEIGHT - height) / 2);
			}
			Global.game.panelLayer.addChild(this);
			opened = true;
		}
		
		public function close():void {
			DisplayUtil.removeChild(parent, this);
			opened = false;
		}
		
		public function dispose():void {
			bg.bitmapData = null;
		}
	}
}